﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BalanceFrm.aspx.cs" Inherits="WebApp.BalanceFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>费用信息</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript">
        var PageSize = 20;
        //页面加载
        $(window).load(function() {
            initData();
            initGrid();
        });

        $(window).resize(function() {
            $('#tb').datagrid('resize');
        });

        function doSearch(pageNumber) {
            if (pageNumber > 0)
                pageNumber = pageNumber - 1; //从0开始
            var params = getParams(pageNumber);
            $('#tb').datagrid('reload', params);
        }

        function getParams(pageNumber) {
            return $.extend({}, {
                DataType: 'Balance',
                SDate: $('#qSDate').datebox('getValue'),
                EDate: $('#qEDate').datebox('getValue'),
                Bank: $('#qBank').val(),
                BType: $('#qFeeType').combobox('getValue'),
                Target: $('#qTarget').combobox('getValue'),
                pageSize: PageSize,
                pageIndex: pageNumber
            });
        }

        function targetFormat(val, rec) {
            if (val) {
                var data = $('#qTarget').combobox('getData');
                for (var i = 0; i < data.length; i++) {
                    if (data[i].id == val)
                        return data[i].fullName;
                }
            }
            return val;
        }

        function balanceTypeFormat(val, rec) {
            if (val) {
                var data = $('#qFeeType').combobox('getData');
                for (var i = 0; i < data.length; i++) {
                    if (data[i].Value == val)
                        return data[i].Name;
                }
            }
            return val;
        }

        function initGrid() {

            var params = getParams(0);
            //加载数据
            $('#tb').datagrid({
                //toolbar: '#searchBar',
                width: 'auto',
                fit: true,
                fitColumns: true,
                height: 'auto',
                striped: true,
                singleSelect: true,
                url: 'JSONHandler.ashx',
                queryParams: params,
                loadMsg: '数据加载中请稍后……',
                pagination: true,
                rownumbers: true,
                columns: [[
        { field: 'bDate', title: '日期', align: 'center', formatter: Common.DateFormatter },
        { field: 'target', title: '客户', align: 'center', formatter: targetFormat },
        { field: 'amount', title: '金额', align: 'center' },
        { field: 'bank', title: '银行', align: 'center' },
        { field: 'desc', title: '描述', align: 'center' },
        { field: 'memo', title: '备注', align: 'center' },
        { field: 'bType', title: '类型', align: 'center', formatter: balanceTypeFormat }
    ]], onDblClickRow: function(rowIndex, rowData) {
        editBalance();
    }, loadFilter: function(data) {
        if (data)
            return data;
        else
            return { "total": 0, "rows": [] };
    }
            });

            $('#tb').datagrid('getPager').pagination({
                pageSize: PageSize, //每页显示的记录条数，默认为10   
                pageList: [10, 20, 30, 40, 50], //可以设置每页记录条数的列表   
                beforePageText: '第', //页数文本框前显示的汉字   
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
                onBeforeRefresh: function() {
                },
                onRefresh: function(pageNumber, pageSize) {
                },
                onChangePageSize: function(pageSize) {
                    PageSize = pageSize;
                },
                onSelectPage: function(pageNumber, pageSize) {
                    doSearch(pageNumber);
                }
            });

        }

        function initData() {
            $('#qSDate').datebox('setValue', DateFormatter(new Date().DateAdd('m', -1)));
            $('#qEDate').datebox('setValue', DateFormatter(new Date()));

            $.getJSON("JSONHandler.ashx?DataType=FeeType", function(result) {
                $('#qFeeType').combobox('loadData', result);
            });

            $.getJSON("JSONHandler.ashx?DataType=Client&AddKey=false", function(result) {
                $('#qTarget').combobox('loadData', result);
            });
        }

        function resetAll() {
            $('#fm').form('clear');
        }

        function editBalance() {
            var row = $('#tb').datagrid('getSelected');
            if (row) {
                var id = row.id;
                var bType = row.bType;
                if (bType)
                    openWin('EditBalanceFrm.aspx?BType=' + bType + '&Id=' + id, "编辑收支信息", 600, 400);
            }
        }

        function addBalance() {
            var bType = $('#qFeeType').combobox('getValue');
            if (!bType)
                bType = 1;
            openWin('EditBalanceFrm.aspx?BType=' + bType, "新增收支信息", 600, 400);
        }

        function deleteBalance() {
            var row = $('#tb').datagrid('getSelected');
            if (row) {
                $.messager.confirm('提示', '确认删除吗?', function(r) {
                    if (r) {
                        var id = row.id;
                        WebApp.BalanceFrm.DeleteBalance(id, function callback(p) {

                            if (p.error) {
                                $.messager.alert('异常消息', '提交删除异常,' + p.error.Message, 'error');
                                return;
                            }
                            var res = p.value;
                            if (res) {
                                var data = res.Data;
                                var msg = res.Msg;
                                if (msg)
                                    $.messager.alert('删除异常', msg, 'error');
                                else {
                                    if (data == true) {
                                        $.messager.alert('消息', '收支信息已删除!', 'info');
                                        refreshParent();
                                    }
                                    else
                                        $.messager.alert('删除收支信息失败', msg, 'error');
                                }
                            }
                        });
                    }
                });
            }
        }

        function openWin(url, title, width, height) {
            $('#winFrame').prop('src', url);
            $('#winFrame').prop('style').height = height + 'px';
            $('#winFrame').prop('style').width = width + 'px';
            $('#win').window({
                title: title,
                width: width + 'px',
                height: height + 'px',
                modal: true
            });
        }      
    </script>

</head>
<body style="padding: 5px">
    <form id="form1" runat="server">
    </form>
    <div class="easyui-layout" fit="true">
        <div data-options="region:'north'" style="padding: 5px;">
            <form id="fm" style="padding: 0; margin: 0">
            <span>时段：</span>
            <input id="qSDate" class="easyui-datebox" data-options="formatter:DateFormatter,parser:DateParser"
                style="width: 90px" />至
            <input id="qEDate" class="easyui-datebox" data-options="formatter:DateFormatter,parser:DateParser"
                style="width: 90px" />
            <span>收支类型：</span>
            <input class="easyui-combobox" id="qFeeType" data-options="valueField:'Value',textField:'Name'"
                style="width: 70px;">
            <span>对象：</span>
            <input class="easyui-combobox" id="qTarget" data-options="valueField:'id',textField:'fullName'"
                style="width: 150px;">
            <span>银行：</span><input type="text" style="width: 80px" id="qBank">
            <a href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="doSearch(0);">
                搜索</a> <a href="#" class="easyui-linkbutton" iconcls="icon-undo" plain="true" onclick="resetAll();">
                    重置</a> <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addBalance();">
                        新增</a> <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteBalance();">
                            删除</a></form>
        </div>
        <div data-options="region:'center'" style="position: relative;">
            <table class="easyui-datagrid" id="tb">
            </table>
        </div>
    </div>
    <div id="win">
        <iframe id="winFrame" src="" style="width: 0px; height: 0px" frameborder="0" scrolling="no">
        </iframe>
    </div>
</body>
</html>
